namespace Stee.CAP8.MessageBus.Sagas.Core.Sagas.Storages.Steps
{
    using Stee.CAP8.MessageBus.Sagas.Core.Sagas.Storages.Dtos.Steps;
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    public class InMemorySagaStepStorage : ISagaStepStorage
    {
        private static readonly ConcurrentDictionary<string, List<SagaStepDataProperty>> StorageList = new ConcurrentDictionary<string, List<SagaStepDataProperty>>();

        public Task DeleteAsync(string id, SagaStepDataProperty sagaDataProperty)
        {
            throw new NotImplementedException();
        }

        public Task<List<SagaStepDataProperty>> FindAsync(string id)
        {
            _ = StorageList.TryGetValue(id, out var values);
            return Task.FromResult(values);
        }

        public List<SagaStepDataProperty> Find(string id)
        {
            _ = StorageList.TryGetValue(id, out var values);
            return values;
        }
        public void Insert(string id, SagaStepDataProperty sagaDataProperty)
        {
            _ = StorageList.TryGetValue(id, out var values);

            values ??= new List<SagaStepDataProperty>() { sagaDataProperty };
            values.Add(sagaDataProperty);
            StorageList.AddOrUpdate(id, values, (id, values) =>
            {

                return values;
            });
        }

        public Task InsertAsync(string id, SagaStepDataProperty sagaDataProperty)
        {
            _ = StorageList.TryGetValue(id, out var values);
            values ??= new List<SagaStepDataProperty>() { sagaDataProperty };
            _ = StorageList.AddOrUpdate(id, values, (id, values) => values);
            return Task.CompletedTask;
        }
    }
}
